import { _decorator, Button, Component, game, Label, Node, resources, Sprite, SpriteFrame, tween, Vec3 } from 'cc';
import { levelBgpng } from './levelBgpng';
const { ccclass, property } = _decorator;

@ccclass('levelEnd')
export class levelEnd extends Component {


    private timeCur = 250

    private cusStart = 0
    onLoad() {
        this.node.getChildByName('levelpage').on(Button.EventType.CLICK, this.callbackLevelpage, this);
        this.node.getChildByName('again').on(Button.EventType.CLICK, this.callbackAgain, this);
        this.node.getChildByName('next').on(Button.EventType.CLICK, this.callbackNext, this);
        this.node.parent.getChildByName('endBg').active = false
        this.node.active = false;

    }

    start() {
        // this.starTwen()
    }

    callbackLevelpage() {
        console.log("关卡界面")
        game.emit('reverLevel')

    }
    callbackAgain() {
        console.log("复活")

    }
    callbackNext() {
        const curLevel = this.node.parent.getChildByName('levelBgpng').getComponent(levelBgpng).getlevelNum
        game.emit('nextLevel')
        console.log('下一关')
    }

    //整体的缓动
    starTwen() {
        //设置整体向上缓动
        this.node.active = true;
        this.node.parent.getChildByName('endBg').getChildByName('Node').active = false
        this.node.getChildByName('showTime').getComponent(Label).string = '时间：' + (600 - this.timeCur)
        tween(this.node).
            set({ position: new Vec3(0, -185, 0) }).
            to(0.5, { position: new Vec3(0, -20, 0) }).
            call(
                () => {
                    this.startShow()
                }
            ).
            start()
    }

    startShow() {
        //第一颗星
        if (this.timeCur > 0) {
            this.cusStart++;
            console.log('返回当前的星星1', this.cusStart)
            resources.load(`img/UI_STAR_NORMAL/spriteFrame`, SpriteFrame, (err, spriteFrame) => {
                if (err) {
                    console.error("结算界面星星 ", err);
                    return;
                }
                console.log("动态加载了游戏界面背景");
                let star1 = this.node.getChildByName('star1')
                star1.getComponent(Sprite).spriteFrame = spriteFrame
                tween(star1)
                    .to(0.5, { scale: new Vec3(1.2, 1.2, 1) }, { easing: 'sineInOut' }) // 放大到1.2倍
                    .to(0.5, { scale: new Vec3(0.8, 0.8, 1) }, { easing: 'sineInOut' }) // 缩小到1倍
                    // .repeatForever() // 设置动画循环播放 3 次
                    .start(); // 开始动画
                if (this.timeCur >= 240) {

                    this.cusStart++;
                    console.log('返回当前的星星2', this.cusStart)
                    let star2 = this.node.getChildByName('star2')
                    star2.getComponent(Sprite).spriteFrame = spriteFrame
                    tween(this.node.getChildByName('star2'))
                        .to(0.5, { scale: new Vec3(1.2, 1.2, 1) }, { easing: 'sineInOut' }) // 放大到1.2倍
                        .to(0.5, { scale: new Vec3(0.8, 0.8, 1) }, { easing: 'sineInOut' }) // 缩小到1倍
                        // .repeatForever() // 设置动画循环播放 3 次
                        .start(); // 开始动画

                    if (this.timeCur > 270) {
                        this.cusStart++;
                        console.log('返回当前的星星3', this.cusStart)
                        let star3 = this.node.getChildByName('star3')
                        star3.getComponent(Sprite).spriteFrame = spriteFrame
                        tween(this.node.getChildByName('star3'))
                            .to(0.5, { scale: new Vec3(1.2, 1.2, 1) }, { easing: 'sineInOut' }) // 放大到1.2倍
                            .to(0.5, { scale: new Vec3(0.8, 0.8, 1) }, { easing: 'sineInOut' }) // 缩小到1倍
                            // .repeatForever() // 设置动画循环播放 3 次
                            .start(); // 开始动画
                    }
                }


            });

        }
    }

    getcusStart() {
        console.log('返回当前的星星', this.cusStart)
        return this.cusStart;
    }

    // onDestroy() {
    //     this.node.getChildByName('levelpage').on(Button.EventType.CLICK, this.callbackLevelpage, this);
    //     this.node.getChildByName('again').on(Button.EventType.CLICK, this.callbackAgain, this);
    //     this.node.getChildByName('next').on(Button.EventType.CLICK, this.callbackNext, this);
    //     }

}


